#include <stdlib.h>
#include <string.h>
#include "commands.h"
#include "command.h"

#include "includes.h"

extern int32_t dataEnabled;

#define COMMANDS_PROMPT (":>")
sCmdUart UsbUart;
sCmdUart SerialUart;
sCmdUart HostUart; // uart on the step/dir pins

CMD_STR(help, "Displays this message");
CMD_STR(debug, "debug commands")
CMD_STR(ledctrl, "led ctrl")
// List of supported commands


sCommand Cmds[] =
    {
        COMMAND(help),
        COMMAND(debug),
				COMMAND(ledctrl),
        {"", 0, ""}, // End of list signal
};
		
// print out the help strings for the commands
static int help_cmd(sCmdUart *ptrUart, int argc, char *argv[])
{
  sCommand cmd_list;
  int i;

  // now let's parse the command
  i = 0;
  memcpy(&cmd_list, &Cmds[i], sizeof(sCommand));
  while (cmd_list.function != 0)
  {

    CommandPrintf(ptrUart, (cmd_list.name));
    CommandPrintf(ptrUart, (" - "));
    CommandPrintf(ptrUart, (cmd_list.help));
    CommandPrintf(ptrUart, ("\n\r"));
    i = i + 1;
    memcpy(&cmd_list, &Cmds[i], sizeof(sCommand));
  }
  return 0;
}
		
static int ledctrl_cmd(sCmdUart *ptrUart, int argc, char *argv[])
{
  uint32_t i,j;
  if (argc >= 1)
  {
    i = atol(argv[0]);
		j = atol(argv[1]);
		switch(i)
		{
			case 1:
				if(j!=0)
					light_control(LIGHT1,LIGHT_ON); 
				else
					light_control(LIGHT1,LIGHT_OFF); 
				break;				
				
				case 2:
					if(j!=0)
						light_control(LIGHT2,LIGHT_ON); 
					else
						light_control(LIGHT2,LIGHT_OFF); 
					break;				
				
				case 3:
					if(j!=0)
						light_control(LIGHT3,LIGHT_ON); 
					else
						light_control(LIGHT3,LIGHT_OFF); 
					break;
		
		}
  }
	return 0;
}

static int debug_cmd(sCmdUart *ptrUart, int argc, char *argv[])
{
	CommandPrintf(&SerialUart,"debug\r\n");
	return 0;
}

#include "hardware_serial.h"

uint8_t kbhit_hw(void)
{
  return HardwareSerial_Available(&Serial);
}
uint8_t getChar_hw(void)
{
	return HardwareSerial_Read(&Serial);
}
uint8_t putch_hw(char data)
{
	return HardwareSerial_Write(&Serial,data);
}


void commandsInit(void)
{
	HardwareSerial_Init(&Serial,UART1);
	HardwareSerial_Begin(&Serial,9600);
	CommandInit(&SerialUart, kbhit_hw, getChar_hw, putch_hw, NULL); // set up the UART structure
	CommandPrintf(&SerialUart,"\n\rPower Up\n\r");
	CommandPrintf(&SerialUart,COMMANDS_PROMPT);
}

int commandsProcess(void)
{
  CommandProcess(&SerialUart, Cmds, ' ', COMMANDS_PROMPT);
	return 0;
}
